﻿//Kong Yang
//Internet Catalog

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using InternetCatalog.Models;

namespace InternetCatalog.Controllers
{
    public class MeMesController : Controller
    {
        private MeMeDBContext db = new MeMeDBContext();

        // GET: MeMes
        public ActionResult Index()
        {
            return View();
            //return View(db.Images.ToList());
        }


      




        public ActionResult FileUpload(HttpPostedFileBase file)
        {

            if (file != null)
            {
                //Database1Entities db = new Database1Entities();
                MeMeDBContext db = new MeMeDBContext();
                string ImageName = System.IO.Path.GetFileName(file.FileName);
                string physicalPath = Server.MapPath("~/lm2/" + ImageName);

                // save image in folder
                //file.SaveAs(physicalPath);
                //db.Images.(physicalPath);
                db.SaveChanges();

                //save new record in database
                MeMe newRecord = new MeMe();

                //public int ID { get; set; }
                //public string Title { get; set; }
                //public string ImageURL { get; set; }
                //public string Description { get; set; }
                //public string Genre { get; set; }


                newRecord.Title = Request.Form["Title"];
                newRecord.ImageURL = Request.Form["ImageURL"];
                newRecord.Description = Request.Form["Description"];
                newRecord.Genre = Request.Form["Genre"];
                //db.tblAs.Add(newRecord);
                db.Images.Add(newRecord);
                db.SaveChanges();

            }
            //Display records
            return RedirectToAction("../MeMes/FullList");
        }

        public ActionResult FullList()
        {
            return View();
        }












        // GET: MeMes/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            MeMe meMe = db.Images.Find(id);
            if (meMe == null)
            {
                return HttpNotFound();
            }
            return View(meMe);
        }

        // GET: MeMes/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: MeMes/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "ID,Title,ImageURL,Description,Genre")] MeMe meMe)
        {
            if (ModelState.IsValid)
            {
                db.Images.Add(meMe);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(meMe);
        }

        // GET: MeMes/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            MeMe meMe = db.Images.Find(id);
            if (meMe == null)
            {
                return HttpNotFound();
            }
            return View(meMe);
        }

        // POST: MeMes/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "ID,Title,ImageURL,Description,Genre")] MeMe meMe)
        {
            if (ModelState.IsValid)
            {
                db.Entry(meMe).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(meMe);
        }

        // GET: MeMes/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            MeMe meMe = db.Images.Find(id);
            if (meMe == null)
            {
                return HttpNotFound();
            }
            return View(meMe);
        }

        // POST: MeMes/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            MeMe meMe = db.Images.Find(id);
            db.Images.Remove(meMe);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
